package wen.recommend.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wen.recommend.dao.UserDao;

//用户登录

public class LoginServlet extends HttpServlet {

	UserDao userDao = new UserDao();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		//获得用户名和密码
		String userName = request.getParameter("username");
		String userPass = request.getParameter("password");
		//验证用户和密码
		boolean isSucc = userDao.validateUser(userName,userPass);
		if(isSucc){
			request.getSession().setAttribute("loginUser", userName);
			//页面跳回首页
			response.sendRedirect("/booksRecommend/bookList");
		}else{
			//处理中文乱码问题
			response.setContentType("text/html;charset=utf-8");
			//如果登陆不成功，提示用户“用户名或密码不正确！”，页面不会发生改变。
			response.getWriter().write("<SCRIPT>alert('用户名或密码不正确！');history.back();</SCRIPT>");


	}
 }
}
